<?php
$this->breadcrumbs = array(
    'Admin',
);

function getBrandUpdateURL() {
    $URL = CHtml::link('<span>Edit Brand</span>', array('brand/update'));
    return $URL;
}

function writeBrandOption($dataBrand) {
    $data = $dataBrand->data;
    $num = count($data);

    for ($i = 0; $i < $num; $i++) {
        $name = $data[$i]->brand_name;
        $id = $data[$i]->brand_id;
        echo '<option value="' . $id . '">' . $name . '</option>';
    }
}

function writeBrandDetailsUpdateOption($dataBrandDetails, $dataBrand) {
    $data = $dataBrandDetails->data;
    $num = count($data);

    $data_Brand = $dataBrand->data;

    for ($i = 0; $i < $num; $i++) {
        $name = $data_Brand[$i]->brand_name;
        $id = $data[$i]->brand_id;
        echo '<option value="' . $id . '">' . $name . '</option>';
    }
}

function writeBrandDetailsCreateOption($dataBrandDetails, $dataBrand) {
    $data = $dataBrandDetails->data;

    $data_Brand = $dataBrand->data;
    $num = count($data_Brand);

    for ($i = 0; $i < $num; $i++) {
        if ($data[$i]->brand_id == '') {
            $name = $data_Brand[$i]->brand_name;
            $id = $data_Brand[$i]->brand_id;
            echo '<option value="' . $id . '">' . $name . '</option>';
        }
    }
}

function writeNewsOption($dataNews) {
    $data = $dataNews->data;
    $num = count($data);


    for ($i = 0; $i < $num; $i++) {
        $name = $data[$i]->news_title;
        $id = $data[$i]->news_id;
        echo '<option value="' . $id . '">' . $name . '</option>';
    }
}

function writeRefOption($dataRef, $dataBrand){
    $data = $dataRef->data;
    $num = count($data);

    $brand_id = '';
    for ($i = 0; $i < $num; $i++) {
        if($data[$i]->brand_id != $brand_id){
            $brand_name = getBrandName($data[$i]->brand_id, $dataBrand);
            echo '<optgroup label="'. $brand_name .'">';
        }
        $name = $data[$i]->ref_name;
        $id = $data[$i]->ref_id;
        echo '<option value="' . $id . '">' . $name . '</option>';
        if($data[$i]->brand_id != $brand_id){
            echo '</optgroup>';
            $brand_id = $data[$i]->brand_id;
        }
    }
}
function getBrandName($id, $dataBrand){
    $data = $dataBrand->data;
    $num = count($data);

    for ($i = 0; $i < $num; $i++) {
        if($data[$i]->brand_id == $id){
            return $data[$i]->brand_name;
            break;
        }
    }
}
?>
<h1><?php echo $this->id; ?></h1>
<script type="text/javascript">
    function redirect(item){
        var value = document.getElementById(item + '_id').value;
        if(isNumber(value)){
            window.location = '<? echo "http://" . Yii::app()->request->getServerName() . Yii::app()->request->baseUrl . "/index.php/" ?>' + item + '<? echo "/update"; ?>' + '/' + value;
        } else {
            alert(item + ' id must be numeric.');
        }
    }
    function create_brandDetail(){
        var value = document.getElementById('create_brandDetails_id').value;
        if(isNumber(value)){
            window.location = '<? echo "http://" . Yii::app()->request->getServerName() . Yii::app()->request->baseUrl . "/index.php/brandDetails/create"; ?>' + '/' + value;
        } else {
            alert('Brand id must be numeric.');
        }
    }
    function create_ref(){
        var value = document.getElementById('ref_brand_id').value;
        if(isNumber(value)){
            window.location = '<? echo "http://" . Yii::app()->request->getServerName() . Yii::app()->request->baseUrl . "/index.php/ref/create"; ?>' + '/' + value;
        } else {
            alert('Brand id must be numeric.');
        }
    }
    function isNumber(n) {
        return !isNaN(parseFloat(n)) && isFinite(n);
    }
</script>
<table>
    <tr>
        <td>
            <b>Brand</b>
        </td>
        <td></td>
    </tr>
    <tr>
        <td>
            <?php echo CHtml::link('<span>Create Brand</span>', array('brand/create')); ?>
        </td>
        <td></td>
    </tr>
    <tr>
        <td><a href="#" onclick="redirect('brand');">Brand Edit</a> : Brand Name :</td>
        <td>
            <select id="brand_id" style="width: 150px">
                <? writeBrandOption($dataBrand); ?>
            </select>
        </td>
    </tr>
    <tr>
        <td><?php echo CHtml::link('<span>Main Train Brand</span>', array('brand/admin')); ?></td>
        <td>
        </td>
    </tr>
    <tr>
        <td><b>Brand Detail</b></td>
        <td></td>
    </tr>
    <tr>
        <td>
            <a href="#" onclick="create_brandDetail('brandDetails');">Create Brand Details</a>
        </td>
        <td>
            <select id="create_brandDetails_id" style="width: 150px">
<? writeBrandDetailsCreateOption($dataBrandDetails, $dataBrand); ?>
            </select>
        </td>
    </tr>
    <tr>
        <td><a href="#" onclick="redirect('brandDetails');">Brand Details Edit</a> : Brand id : </td>
        <td>
            <select id="brandDetails_id" style="width: 150px">
<? writeBrandDetailsUpdateOption($dataBrandDetails, $dataBrand); ?>
            </select>
        </td>
    </tr>
    <tr>
        <td><?php echo CHtml::link('<span>Main Train Brand Details</span>', array('brandDetails/admin')); ?></td>
        <td>
        </td>
    </tr>
    <tr>
        <td><b>News</b></td>
        <td></td>
    </tr>
    <tr>
        <td><?php echo CHtml::link('<span>Create News</span>', array('news/create')); ?></td>
        <td></td>
    </tr>
    <tr>
        <td><a href="#" onclick="redirect('news');">News Edit</a> : News id : </td>
        <td>
            <select id="news_id" style="width: 150px">
<? writeNewsOption($dataNews); ?>
            </select>
        </td>
    </tr>
    <tr>
        <td><?php echo CHtml::link('<span>Main Train News</span>', array('news/admin')); ?></td>
        <td>
        </td>
    </tr>
    <tr>
        <td><b>Reference</b></td>
        <td></td>
    </tr>
    <tr>
        <td><a href="#" onclick="create_ref();">Create Reference on Brand Id</a> :</td>
        <td>
            <select id="ref_brand_id" style="width: 150px">
<? writeBrandOption($dataBrand); ?>
            </select>
        </td>
    </tr>
    <tr>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <td><a href="#" onclick="redirect('ref');">Reference Edit</a> : Reference id : </td>
        <td>
            <select id="ref_id" style="width: 150px">
<? writeRefOption($dataRef, $dataBrand); ?>
            </select>
        </td>
    </tr>
    <tr>
        <td><?php echo CHtml::link('<span>Main Train Reference</span>', array('ref/admin')); ?></td>
        <td>
        </td>
    </tr>
</table>


